﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using MINERP.MODEL.FoundationModel;
using MINERP.BLL;
using MINERP.DAL;


namespace MINERP.BLL.AutoCutBoard
{
    public  class CutBoardBLL
    {

        /// <summary>
        /// 获得打印机列表
        /// </summary>
        /// <returns></returns>
        public List<Printer_Info> GetPrinterList()
        {
            List<Printer_Info> printerList = new List<Printer_Info>();
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                foreach (var item in db.printer.ToList())
                {
                    if (item.PType == "1")
                    {
                        Printer_Info p = new Printer_Info()
                        {
                            PName = item.PName,
                            PrinterID = item.PrinterID,
                            CalibrationData = item.CalibrationData
                        };
                        printerList.Add(p);
                    }
                }
            }
            return printerList;
        }
        /// <summary>
        /// 保存校准信息到打印机
        /// </summary>
        /// <param name="printerInfo"></param>
        public void SaveCalibrationData(Printer_Info printerInfo)
        {
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                printer printer = db.printer.First(p => p.PrinterID == printerInfo.PrinterID);
                printer.CalibrationData = printerInfo.CalibrationData;
                db.SaveChanges();
            }
        }
        /// <summary>
        /// 根据大板ID获得大板信息
        /// </summary>
        /// <param name="boardId"></param>
        /// <returns></returns>
        public Board_Info GetBoardInfo(int boardId)
        {
            Board_Info boardInfo =null;
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                Board board = db.Board.FirstOrDefault(b => b.BoardId == boardId);
                if (board != null)
                {
                    if (!board.printerReference.IsLoaded)
                    {
                        board.printerReference.Load();
                    }
                    boardInfo = new Board_Info()
                    {
                        AutoPoint = board.AutoPoint,
                        BoardId = board.BoardId,
                        CreateTime = board.createTime,
                        DPI = board.DPI,
                        PicPath = board.picPath,
                        PrinterId = board.printer.PrinterID,
                        Status = board.status
                    };
                }
            }
            return boardInfo;
 
        }

        /// <summary>
        /// 根据打印机ID获取打印机信息
        /// </summary>
        /// <param name="printerId"></param>
        /// <returns></returns>
        public Printer_Info GetPrinterInfo(int printerId)
        {
            Printer_Info printerInfo = null;
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                printer printer = db.printer.FirstOrDefault(p=>p.PrinterID==printerId);
                if (printer != null)
                {
                    printerInfo = new Printer_Info()
                    {
                      PrinterID =printer.PrinterID,
                      PName =printer.PName,
                      CalibrationData=printer.CalibrationData,
                      IsTransverse =(bool)printer.IsTransverse
                    };
                }
            }
            return printerInfo;
        }

        /// <summary>
        /// 裁切大板
        /// </summary>
        /// <param name="PointString"></param>
        /// <param name="CoordinateString"></param>
        /// <param name="RotatePrint"></param>
        /// <returns></returns>
        public string VectorPointToMachine(string PointString, string CoordinateString, int ComPort,bool ConfirmDialog)
        {
            string result = string.Empty;
            Machine  mac = new Machine();
            result = mac.VectorPointToMachine(PointString, CoordinateString, ComPort, ConfirmDialog);
            mac.Dispose();
            return result;
           
        }

        /// <summary>
        /// 判断裁切机器是否忙
        /// </summary>
        /// <returns></returns>
        public virtual bool isMachineBusy()
        {
            bool result = false;
            Machine mac = new Machine();
            result = mac.isMachineBusy();
            mac.Dispose();
            return result;
        }

        /// <summary>
        /// 校准机器
        /// </summary>
        /// <param name="ComPort"></param>
        /// <param name="Calibration"></param>
        /// <returns></returns>
        public string CalibrationWirzard(int ComPort, string Calibration)
        {
            string result = string.Empty;
            Machine mac = new Machine();
            result = mac.CalibrationWirzard(ComPort, Calibration);
            mac.Dispose();
            return result;
        }

        /// <summary>
        /// 重置机器
        /// </summary>
        public void ResetMachine()
        {
            Machine mac = new Machine();
            mac.ResetMachine();
            mac.Dispose();
        }

        /// <summary>
        /// 显示相机实时画面
        /// </summary>
        /// <param name="left"></param>
        /// <param name="top"></param>
        /// <param name="Foreground"></param>
        /// <returns></returns>
        public string ShowCameraWindow(int left, int top, bool Foreground)
        {
            string result = string.Empty;
            Machine mac = new Machine();
            result = mac.ShowCameraWindow(left, top, Foreground);
            mac.Dispose();
            return result;
 
        }

        /// <summary>
        ///隐藏相机
        /// </summary>
        /// <returns></returns>
        public string HideCameraWindow()
        {
            string result = string.Empty;
            Machine mac = new Machine();
            result = mac.HideCameraWindow();
            mac.Dispose();
            return result;

        }

    }
}
